﻿using System;
using System.Xml;
using log4net;
using log4net.Config;
using Davian.Model;

namespace Davian
{
    class SysXml
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(SysXml));
        private XmlDocument doc = null;
        private MailAccount mailAccount = null;
        private Postgresql pgDb = null;
        private Sqlserver sqlDb = null;

        public SysXml()
        {
            mailAccount = new MailAccount();
            pgDb = new Postgresql();
            sqlDb = new Sqlserver();
            doc = new XmlDocument();
            doc.Load(Constant.SYS_CONFIG);
        }

        public MailAccount ReadMailAccount()
        {
            XmlNodeList elemList = doc.GetElementsByTagName("mailaccount");
            foreach (XmlNode elem in elemList)
            {
                foreach (XmlNode prop in elem)
                {
                    switch (prop.Name)
                    {
                        case "username": mailAccount.Username = prop.InnerXml; break;
                        case "password": mailAccount.Password = prop.InnerXml; break;
                        case "from": mailAccount.From = prop.InnerXml; break;
                        case "outgoinghost": mailAccount.OutgoingHost = prop.InnerXml; break;
                        case "outgoingport": mailAccount.OutgoingPort = Convert.ToInt32(prop.InnerXml); break;
                        default: break;
                    }
                }
            }

            return mailAccount;
        }

        public Postgresql ReadPostgresql()
        {
            XmlNodeList elemList = doc.GetElementsByTagName("postgresql");
            foreach (XmlNode elem in elemList)
            {
                foreach (XmlNode prop in elem)
                {
                    switch (prop.Name)
                    {
                        case "dbname": pgDb.DbName = prop.InnerXml; break;
                        case "username": pgDb.Username = prop.InnerXml; break;
                        case "password": pgDb.Password = prop.InnerXml; break;
                        case "dbhost": pgDb.DbHost = prop.InnerXml; break;
                        case "dbport": pgDb.DbPort = prop.InnerXml; break;
                        default: break;
                    }
                }
            }

            return pgDb;
        }

        public Sqlserver ReadSqlserver()
        {
            XmlNodeList elemList = doc.GetElementsByTagName("sqlserver");
            foreach (XmlNode elem in elemList)
            {
                foreach (XmlNode prop in elem)
                {
                    switch (prop.Name)
                    {
                        case "server": sqlDb.Server = prop.InnerXml; break;
                        case "database": sqlDb.DataBase = prop.InnerXml; break;
                        case "username": sqlDb.Username = prop.InnerXml; break;
                        case "password": sqlDb.Password = prop.InnerXml; break;
                        default: break;
                    }
                }
            }

            return sqlDb;
        }
    }
}
